package algorithm

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/middleware"
	"github.com/bgmp/model"
	"github.com/bgmp/service"
	"github.com/gin-gonic/gin"
)

func (b Base) Csii(c *gin.Context, req *AgentRequest) *service.CsiiResult {
	userId := middleware.GetUserId(c)
	if req.PatientId != 0 {
		userId = req.PatientId
	}
	//bgs := model.GetBgBefore(config.DB, userId, req.Time, 96)
	//
	//detail := &model.UserDetail{}
	//detail.UserId = userId
	//if err := detail.GetByUserId(config.DB); err != nil {
	//	handler.Errorf(c, err.Error())
	//	return nil
	//}
	//
	//extra := transformExtra(detail)

	list, err := getCsii(userId, req.Time, 96) // service.Csii(bgs, extra, detail.Weight)
	if err != nil {
		handler.Errorf(c, err.Error())
		return nil
	}
	return list
}

func getCsii(userId uint, t model.Time, num int) (*service.CsiiResult, error) {
	bgs := model.GetBgBefore(config.DB, userId, t, num)

	detail := &model.UserDetail{}
	detail.UserId = userId
	if err := detail.GetByUserId(config.DB); err != nil {
		return nil, err
	}

	extra := transformExtra(detail)

	return service.Csii(bgs, extra, detail.Weight)
}
